package com.hxzy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.util.internal.ThreadLocalRandom;
import com.hxzy.common.resp.R;
import com.hxzy.order.req.OrderReq;
import com.hxzy.order.req.OrderSearchReq;
import com.hxzy.order.resp.OrderResp;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/10/12 15:53
 * @Version 1.0
 */
@Log4j2
@RestController
@RequestMapping(value = "/order/bean")
public class OrderBeanController {

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @SentinelResource("/order/bean/findById/{id}")
    @GetMapping(value = "/findById/{id}")
    public R<OrderResp>  findById(@PathVariable(value = "id") Integer id){
        OrderResp  resp=new OrderResp();
        resp.setOrderId(11111);
        resp.setCreateTime(new Date());
        resp.setUserId(123);
        resp.setShopId(234);
        resp.setTotalMoney(23.0);
        return R.okHasData(resp);
    }


    /**
     * 创建订单
     * @param req
     * @return
     */
    @PostMapping(value = "/create")
    public R createOrder(@RequestBody OrderReq req){
        log.info(req.toString());
        return R.okHasData(UUID.randomUUID().toString());
    }



    /**
     * 分页查询
     * @return
     */
    @GetMapping(value = "/data")
    public R   data(OrderSearchReq req) throws InterruptedException {
        Thread.sleep(2000);
        List<OrderResp>  orderRespList=new ArrayList<>();
        for(int i=1;i<=5;i++) {
            OrderResp resp = new OrderResp();
            resp.setOrderId(ThreadLocalRandom.current().nextInt(100000));
            resp.setCreateTime(new Date());
            resp.setUserId(123);
            resp.setShopId(234);
            resp.setTotalMoney(ThreadLocalRandom.current().nextInt(150)+20.0);
            orderRespList.add(resp);
        }

        Map<String,Object>  map=new HashMap<>();
        map.put("total",103);
        map.put("rows",orderRespList);

        return R.okHasData(map);
    }

}
